
from sko.PSO import PSO
import matplotlib.pyplot as plt
import random
import math
from math import e
import numpy as np
#pso优化求参过程以及算法迭代图

plt.rcParams['font.sans-serif']=['STSong']     # 中文宋体
#填涂轮廓
def fun(x):
    x1,x2=x
    xx=random.randint(2100,5300)
    temp=0
    return abs((1-((xx-x1)/(2*x2))**2)-(1.90094999*math.pow(10,-10)*xx**3-2.33433765*math.pow(10,-6)*xx**2+9.01913197*math.pow(10,-3)*xx-1.04800377*math.pow(10,1)))

pop =300
max_iter=400
pso = PSO(fun, n_dim=2, pop=500, max_iter=max_iter,lb=[3310, 560],ub=[3340, 580])
fitness = pso.fit()
y = pso.gbest_y_hist
x = np.arange(0,len(y),1)
plt.xlabel('适应度值',fontdict={ 'size' : 16})
plt.ylabel('迭代次数',fontdict={ 'size' : 16})
plt.plot(x,y)
plt.show()
print('best_x is ',pso.gbest_x)
print('best_y is ',pso.gbest_y)




#未填涂轮廓
'''def fun(x):
    x1,x2=x
    xx=random.randint(400,2100)
    temp=0
    return abs((1-((xx-x1)/(2*x2))**2)-(1.05220158*math.pow(10,-24)*xx**3-1.00388677*math.pow(10,-6)*xx**2+2.65011236*math.pow(10,-3)*xx-7.48976005*math.pow(10,-1)))

pop =300
max_iter=400
pso = PSO(fun, n_dim=2, pop=500, max_iter=max_iter,lb=[1300,450],ub=[1400, 550])
fitness = pso.fit()
y = pso.gbest_y_hist
x = np.arange(0,len(y),1)
plt.xlabel('适应度值',fontdict={ 'size' : 16})
plt.ylabel('迭代次数',fontdict={ 'size' : 16})
plt.plot(x,y)
plt.show()
print('best_x is ',pso.gbest_x)
print('best_y is ',pso.gbest_y)'''


#填涂灰度 
'''def fun(x):
    x1,x2=x
    xx=random.randint(400,2100)
    temp=0
    return abs((1-((xx-x1)/(2*x2))**2)-(5.98137105*math.pow(10,-19)*xx**3-1.15757888*math.pow(10,-3)*xx**2+2.24257098*math.pow(10,-1)*xx-9.86130002*math.pow(10,0)))

pop =300
max_iter=400
pso = PSO(fun, n_dim=2, pop=500, max_iter=max_iter,lb=[90,0],ub=[120, 20])
fitness = pso.fit()
y = pso.gbest_y_hist
x = np.arange(0,len(y),1)
plt.xlabel('适应度值',fontdict={ 'size' : 16})
plt.ylabel('迭代次数',fontdict={ 'size' : 16})
plt.plot(x,y)
plt.show()
print('best_x is ',pso.gbest_x)
print('best_y is ',pso.gbest_y)'''

#未填涂灰度 
'''def fun(x):
    x1,x2=x
    xx=random.randint(400,2100)
    temp=0
    return abs((1-((xx-x1)/(2*x2))**2)-(2.49105470*math.pow(10,-17)*xx**3-3.18339641*math.pow(10,-3)*xx**2+1.10316801*math.pow(10,0)*xx-9.45724251*math.pow(10,1)))

pop =300
max_iter=400
pso = PSO(fun, n_dim=2, pop=500, max_iter=max_iter,lb=[150,0],ub=[180, 10])
fitness = pso.fit()
y = pso.gbest_y_hist
x = np.arange(0,len(y),1)
plt.xlabel('适应度值',fontdict={ 'size' : 16})
plt.ylabel('迭代次数',fontdict={ 'size' : 16})
plt.plot(x,y)
plt.show()
print('best_x is ',pso.gbest_x)
print('best_y is ',pso.gbest_y)'''


#热轧带
# 有缺陷轮廓
'''def fun(x):
    x1,x2=x
    xx=random.randint(0,3000)
    temp=0
    return abs((1-((xx-x1)/(2*x2))**2)-(2.16024551*math.pow(10,-24)*xx**3-2.72304458*math.pow(10,-6)*xx**2+3.76788415*math.pow(10,-3)*xx-3.03407870*math.pow(10,-1)))

pop =300
max_iter=400
pso = PSO(fun, n_dim=2, pop=500, max_iter=max_iter,lb=[1800,350],ub=[1900, 390])
fitness = pso.fit()
y = pso.gbest_y_hist
x = np.arange(0,len(y),1)
plt.xlabel('适应度值',fontdict={ 'size' : 16})
plt.ylabel('迭代次数',fontdict={ 'size' : 16})
plt.plot(x,y)
plt.show()
print('best_x is ',pso.gbest_x)
print('best_y is ',pso.gbest_y)'''

#有缺陷灰度 
'''def fun(x):
    x1,x2=x
    xx=random.randint(0,255)
    temp=0
    return abs((1-((xx-x1)/(2*x2))**2)-(6.74319653*math.pow(10,-18)*xx**3-2.96519485*math.pow(10,-3)*xx**2+7.58094232*math.pow(10,-1)*xx-4.74543928*math.pow(10,1)))

pop =300
max_iter=400
pso = PSO(fun, n_dim=2, pop=500, max_iter=max_iter,lb=[100,35],ub=[120, 45])
fitness = pso.fit()
y = pso.gbest_y_hist
x = np.arange(0,len(y),1)
plt.xlabel('适应度值',fontdict={ 'size' : 16})
plt.ylabel('迭代次数',fontdict={ 'size' : 16})
plt.plot(x,y)
plt.show()
print('best_x is ',pso.gbest_x)
print('best_y is ',pso.gbest_y)'''

#有缺陷对比度 
'''def fun(x):
    x1,x2=x
    xx=random.randint(8000,29000)
    temp=0
    return abs((1-((xx-x1)/(2*x2))**2)-(3.57814985*math.pow(10,-24)*xx**3-1.19314245*math.pow(10,-7)*xx**2+5.77790191*math.pow(10,-3)*xx-6.89500518*math.pow(10,1)))

pop =300
max_iter=400
pso = PSO(fun, n_dim=2, pop=500, max_iter=max_iter,lb=[24190,2680],ub=[24220, 2720])
fitness = pso.fit()
y = pso.gbest_y_hist
x = np.arange(0,len(y),1)
plt.xlabel('适应度值',fontdict={ 'size' : 16})
plt.ylabel('迭代次数',fontdict={ 'size' : 16})
plt.plot(x,y)
plt.show()
print('best_x is ',pso.gbest_x)
print('best_y is ',pso.gbest_y)'''

#无缺陷轮廓 
'''def fun(x):
    x1,x2=x
    xx=random.randint(0,3000)
    temp=0
    return abs((1-((xx-x1)/(2*x2))**2)-(1.29636668*math.pow(10,-23)*xx**3-1.00388677*math.pow(10,-6)*xx**2+2.23997766*math.pow(10,-5)*xx-9.99875048*math.pow(10,-1)))

pop =300
max_iter=400
pso = PSO(fun, n_dim=2, pop=500, max_iter=max_iter,lb=[680,490],ub=[700, 510])
fitness = pso.fit()
y = pso.gbest_y_hist
x = np.arange(0,len(y),1)
plt.xlabel('适应度值',fontdict={ 'size' : 16})
plt.ylabel('迭代次数',fontdict={ 'size' : 16})
plt.plot(x,y)
plt.show()
print('best_x is ',pso.gbest_x)
print('best_y is ',pso.gbest_y)'''

#无缺陷灰度 
'''def fun(x):
    x1,x2=x
    xx=random.randint(0,255)
    temp=0
    return abs((1-((xx-x1)/(2*x2))**2)-(2.75815980*math.pow(10,-16)*xx**3-3.18339641*math.pow(10,-3)*xx**2+1.28141518*math.pow(10,0)*xx-1.27952277*math.pow(10,2)))

pop =300
max_iter=400
pso = PSO(fun, n_dim=2, pop=500, max_iter=max_iter,lb=[120,10],ub=[140, 30])
fitness = pso.fit()
y = pso.gbest_y_hist
x = np.arange(0,len(y),1)
plt.xlabel('适应度值',fontdict={ 'size' : 16})
plt.ylabel('迭代次数',fontdict={ 'size' : 16})
plt.plot(x,y)
plt.show()
print('best_x is ',pso.gbest_x)
print('best_y is ',pso.gbest_y)'''

#无缺陷对比度 
'''def fun(x):
    x1,x2=x
    xx=random.randint(8000,29000)
    temp=0
    return abs((1-((xx-x1)/(2*x2))**2)-(2.95587660*math.pow(10,-11)*xx**3-7.54700053*math.pow(10,-7)*xx**2+1.21071601*math.pow(10,-3)*xx-5.17032693*math.pow(10,1)))

pop =300
max_iter=400
pso = PSO(fun, n_dim=2, pop=500, max_iter=max_iter,lb=[16160,3630],ub=[16180, 3660])
fitness = pso.fit()
y = pso.gbest_y_hist
x = np.arange(0,len(y),1)
plt.xlabel('适应度值',fontdict={ 'size' : 16})
plt.ylabel('迭代次数',fontdict={ 'size' : 16})
plt.plot(x,y)
plt.show()
print('best_x is ',pso.gbest_x)
print('best_y is ',pso.gbest_y)'''